package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.heima.model.common.dtos.ResponseResult.errorResult;

@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {


    /*
    * 删除
    * */
    @Override
    public ResponseResult deleted(Integer id) {
        //参数校验
        if (id == null) {
            return errorResult(500, "id不能为空");
        }
        //查询敏感词
        WmSensitive byId = getById(id);
        if (byId == null) {
            return errorResult(500, "id不存在");
        }

        //删除敏感词
         removeById(id);
        return ResponseResult.okResult(0, "删除成功");


    }

    /*
    * 查询列表
    * */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //参选校验
        if(dto == null){
            return errorResult(500,"参数不能为空");
        }
        //检测分页查询
      dto.checkParam();
        //模糊查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(dto.getName()!= null){
            objectLambdaQueryWrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        page(page,objectLambdaQueryWrapper);

        ResponseResult responseResult = new ResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;

    }


    /*
    *
    * 保存
    * */
    @Override
    public ResponseResult save1(WmSensitive wmSensitive) {
        //参数校验
        if(wmSensitive == null){
            return ResponseResult.errorResult(500,"参数不能为空");
        }
        //已经存在的敏感词不能保存
        WmSensitive wmSensitive1 = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if(wmSensitive1!= null){
            return ResponseResult.errorResult(500,"已经存在的敏感词不能保存");
        }
        //保存敏感词
        wmSensitive1.setCreatedTime(new Date());
        save(wmSensitive);

        return ResponseResult.okResult(0,"保存成功");
    }


    /*
    *
    * 修改
    * */
    @Override
    public ResponseResult update(WmSensitive wmSensitive) {
         //参数校验
        if(wmSensitive == null){
            return ResponseResult.errorResult(500,"参数不能为空");
        }
        //修改
        updateById(wmSensitive);
        return ResponseResult.okResult(0,"修改成功");

    }


}
